// pages/user/index.js
import {
  request
} from '../../utils/util'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    is_new: 0,
    canIUseGetUserProfile: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.setData({
      userInfo: app.globalData.userInfo,
      is_new: app.globalData.is_new
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.userInfo,2321321)
    this.setData({
      userInfo: app.globalData.userInfo,
      is_new: app.globalData.is_new,
    })
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
      wx.getSetting({
        withSubscriptions: true,
        success: (res) => {
          // console.log(res, 444)
          app.globalData.mainSwitch = res.subscriptionsSetting.mainSwitch
        }
      })
      app.globalData.userInfo && wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          // console.log(res)
          request('/api/common/login', '', 'get', {
            code: res.code
          }).then(res => {
            console.log(res, '登录返回');
            app.globalData.token = res.token
            app.globalData.is_new = res.is_new
            // wx.setStorageSync('token', res.token)
            // wx.setStorageSync('is_new', res.is_new)
            if (res.is_new !== 0) {
              request('/api/user/info', res.token).then(res => {
                console.log(res, "config");
                app.globalData.userInfo = res.data
              })
            }
            console.log(res, 'data')
            console.log(res, '登录返回');
            const {
              is_new
            } = res
            this.setData({
              is_new
            })
          })
        }
      })
    }
  },
  // 新版用户授权
  getUserInfoProfile() {
    wx.getUserProfile({
      desc: '用于体验完善的服务流程',
      success: res => {
        // console.log(res, 'xinban')
        // 获取授权得到的
        const {
          encryptedData,
          iv,
          userInfo
        } = res
        wx.login({
          success: res => {
            request('/api/user/info', app.globalData.token, 'put', {
              encryptedData,
              iv
            }).then(res => {
              console.log(res, '授权用户信息返回信息++++++++')
              if (res.code === 200) {
                this.setData({
                  is_new: 2,
                  userInfo: {
                    ...this.data.userInfo,
                    ...userInfo,
                  }
                })
                app.globalData.userInfo = {
                  ...this.data.userInfo,
                  ...userInfo
                }
                app.globalData.is_new = 2
                app.globalData.token = res.data
                wx.setStorageSync('token', res.data)
                if (app.globalData.pid) {
                  request('/api/user/bindingUser?pid=' + app.globalData.pid, app.globalData.token, 'put', ).then(res => {})
                }
              }
            })
          }
        })

      }
    })
  },
  // 授权用户信息
  getUserInfo(e) {
    console.log(2222);
    if (e.detail.errMsg === "getUserInfo:fail auth deny") return
    console.log(e)
    // request('')
    // 获取授权得到的
    const {
      encryptedData,
      iv,
      userInfo
    } = e.detail

    wx.login({
      success: res => {
        request('/api/user/info', app.globalData.token, 'put', {
          encryptedData,
          iv
        }).then(res => {
          console.log(res, '授权用户信息返回信息++++++++')
          if (res.code === 200) {
            this.setData({
              is_new: 2,
              userInfo: {
                ...this.data.userInfo,
                ...userInfo,
              }
            })
            app.globalData.userInfo = {
              ...this.data.userInfo,
              ...userInfo
            }
            app.globalData.is_new = 2
            app.globalData.token = res.data
            wx.setStorageSync('token', res.data)
            if (app.globalData.pid) {
              request('/api/user/bindingUser?pid=' + app.globalData.pid, app.globalData.token, 'put', ).then(res => {})
            }
          }
        })
      }
    })

  },
  // 授权手机号码
  getPhoneNumber(e) {
    if (e.detail.errMsg === "getPhoneNumber:fail user deny") return
    // 获取授权得到的
    const {
      encryptedData,
      iv
    } = e.detail

    // app.globalData.login().then(res => {
    request('/api/user/phone', app.globalData.token, 'post', {
      encryptedData,
      iv
    }).then(res => {
      console.log(res, '授权手机号返回信息++++++++')
      if (res.code === 200) {
        this.setData({
          is_new: 1,
          userInfo: {
            ...this.data.userInfo,
            phone: res.data.phone
          }
        })
        app.globalData.userInfo = {
          ...this.data.userInfo,
          phone: res.data.phone
        }
        app.globalData.is_new = 1
      }
    })
    // })

  },
  link() {
    if (this.data.is_new !== 2) {
      wx.showToast({
        title: '请先授权才能访问哦~',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})